package com.ss.android.common.applog;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.c.SharedPreferenceCompat;

import org.json.JSONObject;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CustomChannelHandler {
    public static boolean a = true;
    public String mAppChannel;
    long mApkCreateTime;
    long mSystemCreateTime;
    boolean mHasSendAppInfo;
    boolean f;
    AtomicBoolean g;
    WeakReference<Context> mContextRef;
    private String mSystemRecordChannel;
    private int mApkShuffixNum;
    private static CustomChannelHandler sInstance;

    private CustomChannelHandler(Context context) {
        this.mAppChannel = "";
        this.mSystemRecordChannel = "";
        this.mApkCreateTime = -1;
        this.mApkShuffixNum = -1;
        this.mSystemCreateTime = -1;
        this.mHasSendAppInfo = false;
        this.f = false;
        this.g = new AtomicBoolean(false);
        this.mContextRef = new WeakReference(context.getApplicationContext());
    }

    public static CustomChannelHandler getInstance(Context context) {
        if (sInstance == null) {
            synchronized (CustomChannelHandler.class) {
                if (sInstance == null) {
                    sInstance = new CustomChannelHandler(context);
                }
            }
        }

        return sInstance;
    }

    public final JSONObject getAppInstallJson() {
        JSONObject jsonObject;
        if ((android.support.design.a.isTextEmpty(this.mAppChannel))
                && (android.support.design.a.isTextEmpty(this.mSystemRecordChannel))
                && this.mApkCreateTime == -1L
                && this.mApkShuffixNum == -1
                && this.mSystemCreateTime == -1L) {
            jsonObject = null;
            return jsonObject;
        }

        jsonObject = new JSONObject();
        try {
            if (!android.support.design.a.isTextEmpty(this.mAppChannel)) {
                jsonObject.put("app_channel", this.mAppChannel);
            }

            if (!android.support.design.a.isTextEmpty(this.mSystemRecordChannel)) {
                jsonObject.put("system_record_channel", this.mSystemRecordChannel);
            }

            if (this.mApkCreateTime != -1L) {
                jsonObject.put("apk_create_time", this.mApkCreateTime);
            }

            if (this.mApkShuffixNum != -1) {
                jsonObject.put("apk_shuffix_num", this.mApkShuffixNum);
            }

            if (this.mSystemCreateTime == -1L) {
                return jsonObject;
            }
            jsonObject.put("system_create_time", this.mSystemCreateTime);
        } catch (Exception exception) {
        }

        return jsonObject;
    }

    private static long getFileLastModified(String path) {
        if (!android.support.design.a.isTextEmpty(path)) {
            try {
                File file = new File(path);
                if (!file.exists()) {
                    return -1L;
                }
                return file.lastModified();
            } catch (Exception exception) {
            }
        }
        return -1L;
    }

    static void getSystemRecordChannel(CustomChannelHandler customChannelHandler) {
        if (Logger.debug()) {
            Logger.d("CustomChannelHandler", "getSystemRecordChannel");
        }

        if (android.support.design.a.isTextEmpty(customChannelHandler.mSystemRecordChannel)) {
            try {
                customChannelHandler.mSystemRecordChannel = com.ss.android.usergrowth.a.a(customChannelHandler.mContextRef.get());
            } catch (Throwable throwable) {
            }

            if (!Logger.debug()) {
                return;
            }
            Logger.d("CustomChannelHandler", "get mSystemRecordChannel = " + customChannelHandler.mSystemRecordChannel);
        }
    }

    public final JSONObject b() {
        if (android.support.design.a.isTextEmpty(this.mSystemRecordChannel)) {
            return null;
        }

        JSONObject jsonObject = new JSONObject();
        try {
            if (android.support.design.a.isTextEmpty(this.mSystemRecordChannel)) {
                return jsonObject;
            }
            jsonObject.put("system_record_channel", this.mSystemRecordChannel);
        } catch (Exception exception) {
        }
        return jsonObject;
    }

    static void getSystemCreateTime(CustomChannelHandler customChannelHandler) {
        int v0 = 0;
        if (Logger.debug()) {
            Logger.d("CustomChannelHandler", "getSystemCreateTime");
        }

        try {
            File v1 = new File("/system/app");
            if (!v1.isDirectory()) {
                return;
            }

            File[] v2 = v1.listFiles();
            ArrayList v3 = new ArrayList();
            int v4 = v2.length;
            int v1_1;
            for (v1_1 = 0; v1_1 < v4; ++v1_1) {
                File v5 = v2[v1_1];
                if ((v5.exists()) && v0 < 5) {
                    ((List) v3).add(Long.valueOf(v5.lastModified() / 1000));
                    ++v0;
                }
            }

            Collections.sort(v3);
            customChannelHandler.mSystemCreateTime = ((Long) ((List) v3).get(((List) v3).size() / 2)).longValue();
            if (!Logger.debug()) {
                return;
            }

            Logger.d("CustomChannelHandler", "get mSystemCreateTime = " + customChannelHandler.mSystemCreateTime);
        } catch (Exception v0_1) {
        }
    }

    public final void saveAppInstallJson() {
        if (this.mContextRef.get() != null) {
            Context context = this.mContextRef.get();
            JSONObject appInstallJson = getInstance(context).getAppInstallJson();
            if (appInstallJson == null) {
                return;
            }

            if (Logger.debug()) {
                Logger.d("CustomChannelHandler", "save appInstallJson = " + appInstallJson);
            }

            try {
                appInstallJson.put("has_send_app_info", this.mHasSendAppInfo);
                SharedPreferences.Editor editor = context.getSharedPreferences("custom_channels", 0).edit();
                editor.putString("app_install_info", appInstallJson.toString());
                SharedPreferenceCompat.commit(editor);
            } catch (Exception e) {

            }
        }
    }

    static void getApkInfo(CustomChannelHandler customChannelHandler) {
        if (Logger.debug()) {
            Logger.d("CustomChannelHandler", "getApkInfo");
        }

        if (customChannelHandler.mContextRef.get() == null) {
            return;
        }

        String publicSourceDir = null;
        try {
            publicSourceDir = customChannelHandler.mContextRef
                    .get()
                    .getPackageManager()
                    .getApplicationInfo(customChannelHandler.mContextRef.get().getPackageName(),
                            0).publicSourceDir;
            customChannelHandler.mApkCreateTime = getFileLastModified(publicSourceDir) / 1000;
            if (Logger.debug()) {
                Logger.d("CustomChannelHandler", "get mApkCreateTime = " + customChannelHandler.mApkCreateTime);
            }
        } catch (PackageManager.NameNotFoundException v1_1) {
        }

        if (publicSourceDir != null) {
            try {
                Matcher matcher = Pattern.compile("(.*)-(\\d+)(.*)").matcher(publicSourceDir.trim());
                customChannelHandler.mApkShuffixNum = matcher.find() ? Integer.parseInt(matcher.group(2)) : -1;
                if (!Logger.debug()) {
                    return;
                }
                Logger.d("CustomChannelHandler", "get mApkSuffixNum = " + customChannelHandler.mApkShuffixNum);
            } catch (Exception exception) {
            }
        }
    }

    public final void loadAppInstallJsonFromLocal() {
        if (this.mContextRef.get() != null) {
            Context context = this.mContextRef.get();
            try {
                JSONObject jsonObject = new JSONObject(context.getSharedPreferences("custom_channels", 0)
                        .getString("app_install_info", ""));
                CustomChannelHandler customChannelHandler = getInstance(context);
                try {
                    customChannelHandler.mAppChannel = jsonObject.optString("app_channel", "");
                    customChannelHandler.mSystemRecordChannel = jsonObject.optString("system_record_channel", "");
                    customChannelHandler.mApkCreateTime = jsonObject.optLong("apk_create_time", -1);
                    customChannelHandler.mApkShuffixNum = jsonObject.optInt("apk_shuffix_num", -1);
                    customChannelHandler.mSystemCreateTime = jsonObject.optLong("system_create_time", -1);
                } catch (Exception exception) {
                }
                if (Logger.debug()) {
                    Logger.d("CustomChannelHandler", "load appInstallJson = " + jsonObject);
                }
                this.mHasSendAppInfo = jsonObject.optBoolean("has_send_app_info", false);
            } catch (Throwable throwable) {
            }
        }
    }

    static AtomicBoolean d(CustomChannelHandler arg1) {
        return arg1.g;
    }
}

